<?php
namespace App\Admin\Controllers\OverSeasIn;

use App\Models\Basics\SysArea;
use App\Models\OverSeasIn\Delivery;
use App\Models\OverSeasBa\Warehouse;
use App\Models\Sys\Logs;
use App\Util\User;
use Encore\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\Request;
use App\Models\Basics\ExpressCompany;
use App\Models\Basics\ExpressChannel;
use Symfony\Component\HttpFoundation\StreamedResponse;

class OSTrackExController extends AdminController{

    protected $title = 'CN至海外仓(中转)-运单号异常';

    protected $filters = [
        'start_date' => null,
        'end_date' => null,
        'delivery_no' => null,
        'warehouse_id' => null,
        'area_id' => null,
        'seller_id' => null,
        'status' => null,
        'transport_status' => null,
        'sku' => null,
    ];

    protected function grid()
    {
        $filtersField = array_keys($this->filters);
        $filters = $this->_getFilters($filtersField);
        $this->_initFilters($filters);
        $query = $this->_initQuery($filters);
        $paginator = $this->_paginator($query);
        $action = getAction($filtersField);
        $filtersBlade = [];
        $filtersBlade['start_date'] = $this->filters['start_date'];
        $filtersBlade['end_date'] = $this->filters['end_date'];
        $filtersBlade['delivery_no'] = $this->filters['delivery_no'];
        $filtersBlade['warehouse_id'] = getOptions(Warehouse::options(),$this->filters['warehouse_id']);
        $filtersBlade['area_id'] = getOptions(SysArea::options(),$this->filters['area_id']);
        $filtersBlade['seller_id'] = getOptions(User::getSellerOptions(),$this->filters['seller_id']);
        $filtersBlade['status'] = getOptions(Delivery::$_status,$this->filters['status']);
        $filtersBlade['transport_status'] = getOptions(Delivery::$_transportStatus,$this->filters['transport_status']);
        $filtersBlade['sku'] = $this->filters['sku'];
        $downloadUrl = '/xqc/overseas-in/os-track-ex-download?' . http_build_query(array_merge(Request::all(),['_export_' => true]));
        return view('overseas-in.os-track-ex.index', compact('paginator','action','filtersBlade','downloadUrl'));
    }

    private function _getFilters($filtersField){
        $filters = [];
        foreach ($filtersField as $field){
            $filters[$field] = request($field);
        }
        return $filters;
    }

    private function _initFilters($filters){
        foreach ($filters as $field => $filter){
            $this->filters[$field] = $filter;
        }
    }

    private function _initQuery($filters){
        $query = Delivery::query()
            ->where('delivery_type', Delivery::OS_DELIVERY_DROPSHIPPING) //海外仓-中转
            ->where('track_ex_status',1)
            ->orderBy('id', 'DESC');
        foreach ($filters as $field => $filter){
            if(!$filter)continue;
            if($field == 'start_date'){
                $query->where('delivered_at','>=',$filter.' 00:00:00');
            }
            if($field == 'end_date'){
                $query->where('delivered_at','<=',$filter.' 23:59:59');
            }
            if(in_array($field,['delivery_no','warehouse_id','area_id','seller_id','status','transport_status'])){
                $query->where($field,$filter);
            }
            if($field == 'sku'){
                $query->whereHas('detail.sku', function ($query) use($filter){
                    $query->where('psku_code', 'like', "%{$filter}%")
                        ->orWhere('psku_cn', 'like', "%{$filter}%");
                });
            }
        }
        return $query;
    }

    private function _paginator($query){
        return $query->paginate((int) (request("per_page",10)))->appends(Request::input());
    }

    protected function detail($id)
    {
        $delivery = Delivery::query()->findOrFail($id);
        $logs = Logs::getLogs('os-in-delivery',$id);
        return view('overseas-in.os-track-ex-detail.index', compact('id','delivery','logs'));
    }

    public function download(){
        $filtersField = array_keys($this->filters);
        $filters = $this->_getFilters($filtersField);
        $query = $this->_initQuery($filters);
        $this->_exeDownload($query);
    }

    private function _exeDownload($query){
        $head = ['发货单号','物流公司ID','物流渠道ID','销售员','目的仓库','站点','状态','备注','发货时间','物流公司','物流渠道','追踪号','发货公斤数','运费'];
        set_time_limit(0);
        $response = new StreamedResponse(null, 200, [
            'Content-Type'        => 'text/csv;charset=utf-8',
            'Content-Disposition' => 'attachment;filename="中转运单号异常.csv"',
        ]);
        $response->setCallback(function () use ($head, $query) {
            $fp = fopen('php://output', 'w');
            fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
            fputcsv($fp, $head);
            $query->orderBy('id','DESC')->chunk(500, function ($re) use ($fp){
                $data = self::getDownloadData($re);
                foreach ($data as $item) {
                    fputcsv($fp, $item);
                }
            });
            fclose($fp);
        });
        $response->send();
        exit(0);
    }

    public static function getDownloadData($re){
        foreach ($re as $row){
            $temp = [
                $row->delivery_no,
                $row->logistics->express_company_id ?? '',
                $row->logistics->express_channel_id ?? '',
                User::getUsernameById($row->seller_id),
                $row->warehouse->w_code ?? '',
                $row->area->area ?? '',
                Delivery::$_status[$row->status],
                $row->remarks,
                $row->delivered_at,
                ExpressCompany::getNameById($row->logistics->express_company_id ?? null),
                ExpressChannel::getNameById($row->logistics->express_channel_id ?? null),
                $row->logistics->tracking_no ?? '',
                $row->logistics->total_weight ?? '',
                $row->logistics->shipping_fee ?? '',
            ];
            yield $temp;
        }
    }
}
